parameters:
  osGroup: ''
  osSubgroup: ''
  archType: ''
  buildConfig: ''
  runtimeFlavor: ''
  helixQueues: ''
  targetRid: ''
  nameSuffix: ''
  platform: ''
  shouldContinueOnError: ''
  rootFolder: ''
  includeRootFolder: ''
  displayName: ''
  artifactName: ''
  archiveExtension: ''
  archiveType: ''
  tarCompression: ''


steps:
  # There is a global.json in the runtime repo, but it sets the version to 7.xxx preview. This creates a local global.json to allow the running of 6.0 version.
  - script: |
      echo '{}' > ./global.json 
    displayName: Create global.json
    workingDirectory: $(Build.SourcesDirectory)

  # Get the current maui nuget config so all things can be found and darc based package sources are kept up to date.
  - script: |
      curl -o NuGet.config 'https://raw.githubusercontent.com/dotnet/maui/net6.0/NuGet.config'
      curl -o dotnet-install.sh 'https://dotnet.microsoft.com/download/dotnet/scripts/v1/dotnet-install.sh'
      chmod -R a+rx .
      ./dotnet-install.sh --channel 6.0 --quality daily --install-dir .
      ./dotnet --info
      ./dotnet workload install maui --from-rollback-file https://aka.ms/dotnet/maui/net6.0.json --configfile NuGet.config
    displayName: Install MAUI workload
    workingDirectory: $(Build.SourcesDirectory)

  - script: $(Build.SourcesDirectory)/eng/testing/performance/create-provisioning-profile.sh
    displayName: Create iOS code signing and provisioning profile

  - script: |
      ./dotnet new maui -n MauiTesting
      cd MauiTesting
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.props.net6 ./Directory.Build.props
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.targets.net6 ./Directory.Build.targets
      cp $(Build.SourcesDirectory)/NuGet.config ./NuGet.config
    displayName: Setup MAUI Project
    workingDirectory: $(Build.SourcesDirectory)

  - script: |
      chmod -R a+r .
      # Restore is split out because of https://github.com/dotnet/sdk/issues/21877, can be removed with --no-restore once fixed
      ../dotnet restore
      ../dotnet publish -bl:MauiAndroid.binlog -f net6.0-android -c Release -r android-arm64 --no-restore --self-contained
      mv ./bin/Release/net6.0-android/android-arm64/com.companyname.mauitesting-Signed.apk ./MauiAndroidDefault.apk
    displayName: Build MAUI Android
    workingDirectory: $(Build.SourcesDirectory)/MauiTesting

    # This step pulls the product version from the used Microsoft.Maui.dll file properties and saves it for upload with the maui test counter.
    # We pull from this file as we did not find another place to reliably get the version information pre or post build.
  - powershell: |
      $RetrievedMauiVersion = Get-ChildItem .\obj\Release\net6.0-android\android-arm64\linked\Microsoft.Maui.dll | Select-Object -ExpandProperty VersionInfo | Select-Object ProductVersion | Select-Object -ExpandProperty ProductVersion
      $RetrievedMauiVersion
      Write-Host "##vso[task.setvariable variable=mauiVersion;isOutput=true]$RetrievedMauiVersion"
    name: getMauiVersion
    displayName: Get and Save MAUI Version
    workingDirectory: $(Build.SourcesDirectory)/MauiTesting

  - script: |
      set -x
      pwd
      git clone https://github.com/microsoft/dotnet-podcasts.git -b maui/perf --single-branch
      cd dotnet-podcasts
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.props.net6 ./Directory.Build.props
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.targets.net6 ./Directory.Build.targets
    displayName:  Clone podcast app
    workingDirectory: $(Build.SourcesDirectory)

  - script: |
      chmod -R a+r .
      # remove net6.0-maccatalyst to work around https://github.com/dotnet/sdk/issues/21877
      cp MauiTesting.csproj MauiTesting.csproj.bak
      sed -i'' -e 's/net6.0-ios;net6.0-maccatalyst/net6.0-ios/g' MauiTesting.csproj

      ../dotnet publish -bl:MauiiOS.binlog -f net6.0-ios --self-contained -r ios-arm64 -c Release /p:_RequireCodeSigning=false /p:ApplicationId=net.dot.mauitesting
      mv ./bin/Release/net6.0-ios/ios-arm64/publish/MauiTesting.ipa ./MauiiOSDefault.ipa

      cp MauiTesting.csproj.bak MauiTesting.csproj
    displayName: Build MAUI Default iOS
    workingDirectory: $(Build.SourcesDirectory)/MauiTesting

  - script: |
      chmod -R a+r .
      # remove net6.0-maccatalyst to work around https://github.com/dotnet/sdk/issues/21877
      cp Microsoft.NetConf2021.Maui.csproj Microsoft.NetConf2021.Maui.csproj.bak
      sed -i'' -e 's/net6.0-ios;net6.0-maccatalyst/net6.0-ios/g' Microsoft.NetConf2021.Maui.csproj

      ../../../dotnet build ../Web/Components/Podcast.Components.Maui.csproj
      ../../../dotnet publish Microsoft.NetConf2021.Maui.csproj -bl:MauiiOSPodcast.binlog -f net6.0-ios --self-contained -r ios-arm64 -c Release /p:_RequireCodeSigning=false /p:ApplicationId=net.dot.netconf2021.maui
      mv ./bin/Release/net6.0-ios/ios-arm64/publish/Microsoft.NetConf2021.Maui.ipa ./MauiiOSPodcast.ipa

      cp Microsoft.NetConf2021.Maui.csproj.bak Microsoft.NetConf2021.Maui.csproj
    displayName: Build MAUI Podcast iOS
    workingDirectory: $(Build.SourcesDirectory)/dotnet-podcasts/src/Mobile

  - script: |
      chmod -R a+r .
      ../dotnet publish -bl:MauiMacCatalyst.binlog -f net6.0-maccatalyst -c Release
      mv ./bin/Release/net6.0-maccatalyst/maccatalyst-x64/MauiTesting.app ./MauiMacCatalystDefault.app
    displayName: Build MAUI MacCatalyst
    workingDirectory: $(Build.SourcesDirectory)/MauiTesting

  - script: |
      ./dotnet new maui-blazor -n MauiBlazorTesting
      cd MauiBlazorTesting
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.props.net6 ./Directory.Build.props
      cp $(Build.SourcesDirectory)/src/tests/Common/maui/MauiScenario.targets.net6 ./Directory.Build.targets
      cp $(Build.SourcesDirectory)/NuGet.config ./NuGet.config

      echo -e "using Microsoft.AspNetCore.Components;
        #if ANDROID
          using Android.App;
        #endif

        namespace MauiBlazorTesting.Pages
        {
            public partial class Index
            {
                protected override void OnAfterRender(bool firstRender)
                {
                    if (firstRender)
                    {
                        #if ANDROID
                          var activity = MainActivity.Context as Activity;
                          activity.ReportFullyDrawn();
                        #else
                          System.Console.WriteLine(\"__MAUI_Blazor_WebView_OnAfterRender__\");
                        #endif
                    }
                }
            }
        }" > Pages/Index.razor.cs

      sed -i'' -e "s/{/{\npublic static Android.Content.Context Context { get; private set; }\npublic MainActivity() { Context = this; }/g" Platforms/Android/MainActivity.cs
    displayName: Setup MAUI Blazor Hybrid Project
    workingDirectory: $(Build.SourcesDirectory)

  - script: |
      chmod -R a+r .
      # remove net6.0-maccatalyst to work around https://github.com/dotnet/sdk/issues/21877
      cp MauiBlazorTesting.csproj MauiBlazorTesting.csproj.bak
      sed -i'' -e 's/net6.0-ios;net6.0-maccatalyst/net6.0-ios/g' MauiBlazorTesting.csproj

      # NuGet.config file cannot be in the build directory currently due to https://github.com/dotnet/aspnetcore/issues/41397
      rm NuGet.config

      ../dotnet publish -bl:MauiBlazoriOS.binlog -f net6.0-ios --self-contained -r ios-arm64 -c Release /p:_RequireCodeSigning=false /p:ApplicationId=net.dot.mauiblazortesting
      mv ./bin/Release/net6.0-ios/ios-arm64/publish/MauiBlazorTesting.ipa ./MauiBlazoriOSDefault.ipa

      # Restore NuGet.config
      cp $(Build.SourcesDirectory)/NuGet.config ./NuGet.config

      cp MauiBlazorTesting.csproj.bak MauiBlazorTesting.csproj
    displayName: Build MAUI Blazor iOS
    workingDirectory: $(Build.SourcesDirectory)/MauiBlazorTesting

  - script: |
      chmod -R a+r .

      # NuGet.config file cannot be in the build directory currently due to https://github.com/dotnet/aspnetcore/issues/41397
      rm NuGet.config

      ../dotnet publish -bl:MauiBlazorMacCatalyst.binlog -f net6.0-maccatalyst -c Release

      # Restore NuGet.config
      cp $(Build.SourcesDirectory)/NuGet.config ./NuGet.config

      mv ./bin/Release/net6.0-maccatalyst/maccatalyst-x64/MauiBlazorTesting.app ./MauiBlazorMacCatalystDefault.app
    displayName: Build MAUI Blazor MacCatalyst
    workingDirectory: $(Build.SourcesDirectory)/MauiBlazorTesting

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiAndroid binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/MauiTesting/MauiAndroid.binlog
      artifactName:  ${{ parameters.artifactName }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiiOS binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/MauiTesting/MauiiOS.binlog
      artifactName:  ${{ parameters.artifactName }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiMacCatalyst binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/MauiTesting/MauiMacCatalyst.binlog
      artifactName:  ${{ parameters.artifactName }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiBlazoriOS binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/MauiBlazorTesting/MauiBlazoriOS.binlog
      artifactName:  ${{ parameters.artifactName }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiBlazorMacCatalyst binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/MauiBlazorTesting/MauiBlazorMacCatalyst.binlog
      artifactName:  ${{ parameters.artifactName }}

  - task: PublishBuildArtifacts@1
    displayName: 'Publish MauiiOSPodcast binlog'
    condition: always()
    inputs:
      pathtoPublish: $(Build.SourcesDirectory)/dotnet-podcasts/src/Mobile/MauiiOSPodcast.binlog
      artifactName:  ${{ parameters.artifactName }}

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/MauiTesting/MauiAndroidDefault.apk
        includeRootFolder: true
        displayName: Maui Android App
        artifactName: MauiAndroidAppNet6
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/MauiTesting/MauiiOSDefault.ipa
        includeRootFolder: true
        displayName: Maui iOS IPA
        artifactName: MauiiOSDefaultIPANet6
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/MauiBlazorTesting/MauiBlazoriOSDefault.ipa
        includeRootFolder: true
        displayName: Maui Blazor iOS IPA
        artifactName: MauiBlazoriOSDefaultIPANet6
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/dotnet-podcasts/src/Mobile/MauiiOSPodcast.ipa
        includeRootFolder: true
        displayName: Maui iOS Podcast IPA
        artifactName: MauiiOSPodcastIPANet6
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/MauiTesting/MauiMacCatalystDefault.app
        includeRootFolder: true
        displayName: Maui MacCatalyst App
        artifactName: MauiMacCatalystDefaultNet6
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
        rootFolder: $(Build.SourcesDirectory)/MauiBlazorTesting/MauiBlazorMacCatalystDefault.app
        includeRootFolder: true
        displayName: Maui Blazor MacCatalyst App
        artifactName: MauiBlazorMacCatalystDefaultNet6
        archiveExtension: '.tar.gz'
        archiveType: tar
        tarCompression: gz

  - script: rm -r -f ./bin
    workingDirectory: $(Build.SourcesDirectory)/MauiTesting
    displayName: Clean MauiTesting bin directory
    condition: succeededOrFailed()

  - script: rm -r -f ./bin
    workingDirectory: $(Build.SourcesDirectory)/MauiBlazorTesting
    displayName: Clean MauiBlazorTesting bin directory
    condition: succeededOrFailed()

  - template: /eng/pipelines/common/upload-artifact-step.yml
    parameters:
      osGroup: ${{ parameters.osGroup }}
      osSubgroup: ${{ parameters.osSubgroup }}
      archType: ${{ parameters.archType }}
      buildConfig: ${{ parameters.buildConfig }}
      runtimeFlavor: ${{ parameters.runtimeFlavor }}
      helixQueues: ${{ parameters.helixQueues }}
      targetRid: ${{ parameters.targetRid }}
      nameSuffix: ${{ parameters.nameSuffix }}
      platform: ${{ parameters.platform }}
      shouldContinueOnError: ${{ parameters.shouldContinueOnError }}
      rootFolder:  ${{ parameters.rootFolder }}
      includeRootFolder:  ${{ parameters.includeRootFolder }}
      displayName:  ${{ parameters.displayName }}
      artifactName:  ${{ parameters.artifactName }}
      archiveExtension:  ${{ parameters.archiveExtension }}
      archiveType:  ${{ parameters.archiveType }}
      tarCompression:  ${{ parameters.tarCompression }}
